package event;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class Example_6_5_Dialog_1 {

    JFrame jf = new JFrame("测试Dialog");
    private JButton msgBtn = new JButton("消息对话框");
    private JButton confirmBtn = new JButton("确认对话框");
    private JButton inputBtn = new JButton("输入对话框");
    private JButton optionBtn = new JButton("选项对话框");
    private JButton colorBtn = new JButton("颜色对话框");

    private Icon[] icon = new Icon[]{new ImageIcon("icon/1.gif"),
            new ImageIcon("icon/2.gif"),
            new ImageIcon("icon/3.gif"),
            new ImageIcon("icon/4.gif")
    };

    public Example_6_5_Dialog_1() {
        jf.setBounds(100, 100, 310, 220);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));

        msgBtn.addActionListener(new ShowAction());
        confirmBtn.addActionListener(new ShowAction());
        inputBtn.addActionListener(new ShowAction());
        optionBtn.addActionListener(new ShowAction());
        colorBtn.addActionListener(new ShowAction());

        jf.add(msgBtn);
        jf.add(confirmBtn);
        jf.add(inputBtn);
        jf.add(optionBtn);
        jf.add(colorBtn);

        jf.setVisible(true);
    }

    private class ShowAction implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent event) {
            String title = event.getActionCommand();
            if (title.equals("消息对话框")) {
                JOptionPane.showMessageDialog(jf, "Dialog 消息", "消息对话框", JOptionPane.WARNING_MESSAGE);
            } else if (title.equals("确认对话框")) {
                JOptionPane.showConfirmDialog(jf, "你确定吗？", "确认对话框", JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);
            } else if (title.equals("输入对话框")) {
                JOptionPane.showInputDialog(jf, "请输入信息", "输入对话框", JOptionPane.ERROR_MESSAGE);
            } else if (title.equals("选项对话框")) {
                JOptionPane.showOptionDialog(jf, "Show Option Dialog!"
                        , "选项对话框", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE
                        , null, icon, icon[0]);
            } else if (title.equals("颜色对话框")) {
                JColorChooser.showDialog(jf, "选择你喜欢的颜色", new Color(0, 0, 255));
            }
        }

    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        new Example_6_5_Dialog_1();
    }

}
